package com.zll.ccp.handler;

import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 同意异常处理（目前主要处理权限异常）
 * @author zll
 */
public class MyExceptionHandler implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        ModelAndView modelAndView = new ModelAndView();
        Map<String, Object> result = new HashMap<>(4);
        if (e instanceof UnauthorizedException) {
            result.put("success", false);
            result.put("message", "抱歉您无此权限");

        }else {
            result.put("success", false);
            result.put("message", "系统错误请联系管理员");
        }
        FastJsonJsonView view = new FastJsonJsonView();
        view.setAttributesMap(result);
        modelAndView.setView(view);
        return modelAndView;
    }
}
